package com.k1998.mybatis.controller;

import com.alibaba.fastjson.JSON;
import com.k1998.mybatis.bean.Grade;
import com.k1998.mybatis.bean.GradeStudent;
import com.k1998.mybatis.utils.QueryInfo;
import com.k1998.mybatis.dao.IGradeStudentDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@Api(value = "成绩管理Api文档" , tags = {"成绩查询Api文档"})
public class GradeStudentController {
    @Autowired
    private IGradeStudentDao iGradeStudentDao;

    @PostMapping("/allGradeStudent")
    @ApiOperation(value = "查询成绩", notes="查询成绩")
    public String getAll(@RequestBody QueryInfo queryInfo) {
        // 查询到的结果数量
        int numbers = iGradeStudentDao.getGradeCounts("%"+queryInfo.getQuery()+"%");
//       第几条数据开始
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<GradeStudent> students = iGradeStudentDao.findGrade("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",students);
        res.put("code","success");
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @PostMapping("/detailGradeStudent")
    public String detailGradeStudent(@RequestBody Grade grade) {
        GradeStudent g =  iGradeStudentDao.detailGrade(grade);
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",g);
        res.put("code","success");
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @PostMapping("/insertGrade")
    @ApiOperation(value = "添加成绩个人信息", notes="添加成绩个人信息", httpMethod = "POST")
    public String insertGrade(@RequestBody Grade grade){
        int i = iGradeStudentDao.insertGrade(grade);
        String str = i >0?"success":"error";
        return str;
    }

    @PostMapping("/updateGrade")
    @ApiOperation(value = "修改成绩个人信息", notes="修改成绩个人信息", httpMethod = "POST")
    public String updateGrade(@RequestBody Grade grade){
        int i = iGradeStudentDao.updateGrade(grade);
        String str = i >0?"success":"error";
        return str;
    }

    @PostMapping("/deleteGrade")
    @ApiOperation(value = "删除成绩个人信息", notes="删除成绩个人信息", httpMethod = "POST")
    public String deleteGrade(@RequestBody Grade grade){
        int i = iGradeStudentDao.deleteGrade(grade);
        String str = i >0?"success":"error";
        return str;
    }

}
